<?php

$GLOBALS['html_input_attributes']=Array(
	'wysiwyg','onchange','type','value','style','class','size','name',
	'checked','onblur','onclick','wysiwyg','title','local','table','readonly','placeholder','pointer','uid',
);
$GLOBALS['html_tag_attributes']=Array(
	'wysiwyg','onchange','type','value','style','class','size','width','pointer','uid',
	'height','name','checked','onclick','action','wysiwyg','title','local','table','readonly','placeholder',
	'href','method','action',
);

function H1($s){return "<h1>$s</h1>";};
function H2($s){return "<h2>$s</h2>";};
function H3($s){return "<h3>$s</h3>";};
function B($s){return "<b>$s</b>";}

function get_html_options($p){
	global $db;
	$p[P0]=$p['join']['table'];
	if(!preg_match('/`.*`\.`(.*)`/',$p['query'],$out))return Array();
	$p[P1]=$out[1];
	$q=" SELECT `".$p['join']['column']."` as _id_,".$p['query']." as 'val' FROM ".$p['join']['table']." WHERE 1 ";
	if(isset($p['join']['condition']))$q.=" AND ".$p['join']['condition'];	
	$q.=" GROUP BY ".$p['query']." ";
	$q.=" ORDER BY ".$p['query']." ";
	
	
	
	
	$rs=$db->query($q);
	$o=Array();
	while($row=$rs->fetch_assoc())$o[$row['_id_']]=$row['val'];
	return $o;
}

function html_input_none($p){
	return '';

}

function html_input_datetime($p){
	return html_input($p);
}

function html_input_radio($p){
	$p['class']='radio';
	return html_input($p);
}

function html_tag($p){
	//print_var($p);
	ob_start();
	if(!isset($p['closing']))$p['closing']=0;
	echo '<'.($p['closing']?'/':'').$p['tagName'];
	if($p['closing']){
		echo '>';
		$o=ob_get_contents();
		ob_end_clean();
		return $o;
	}	
	if($p['tagName']=='input')$a=&$GLOBALS['html_input_attributes'];else $a=&$GLOBALS['html_tag_attributes'];	
	foreach($p as $k=>$v){
		if(in_array($k,$a))
			echo(" $k='$v'");
	};
	echo ' ';	
	
	if(!isset($p['selfclosing']))$p['selfclosing']=0;
	
	if($p['selfclosing'])echo '/';
	echo '>';
	if(isset($p['innerHTML']))echo $p['innerHTML'].'</'.$p['tagName'].'>';
	$o=ob_get_contents();
	ob_end_clean();
	return $o;
};

function html_input($p){
	if(!isset($p['tagName']))$p['tagName']='input';
	if(!isset($p['class']))$p['class']='dbinput';
	if(isset($p['secure']))$p['type']='password';
	else
	if(!isset($p['type']))$p['type']='text';
	if(!isset($p['value'])){
		if(isset($p['default']))$p['value']=$p['default'];else $p['value']='';
	}
	
	if($p['type']=='checkbox'){
		$p['class']='checkbox '.str_replace('dbinput','',$p['class']);
		if(isset($p['checked']) && !$p['checked'])unset($p['checked']);
	}
	if($p['type']!='textarea'){
		$p['selfclosing']=1;
		if(!isset($p['value']))$p['value']='';
	}else{
		if(isset($p['value'])){
			$p['innerHTML']=$p['value'];
			unset($p['value']);
		}
	}
	if(isset($p['join'])){
		//$p['local']=$p['join']['local'];
		$p['options']=get_html_options($p);
		//print_var($p,'$p');
		$p['onchange']='$("input[name='.$p['join']['local'].']").attr("value",this.value);';
		$p['tagName']='select';		
		$p['innerHTML']='<option></option>';
		//$o=html_tag($p);
		$p['selfclosing']=0;
		foreach($p['options'] as $k=>$v){
			$selected='';
			if(($p['value']==$k) || ($p['value']==$v))$selected='SELECTED';
			$p['innerHTML'].="<option value='$k' $selected>$v</option>";
		};
	}else{
		if(isset($p['options'])){
			$p['tagName']='select';
			
			$p['innerHTML']='<option></option>';
			foreach($p['options'] as $op){
				$selected='';
				if(($p['value']==$op) || ($p['value']==$op))$selected='SELECTED="1"';
				$p['innerHTML'].="<option value='$op' $selected>$op</option>";				
			}
			if(isset($p['size']))unset($p['size']);		
		}

	};
	if($p['type']=='submit')$p['class'].=' submit';
	return html_tag($p);
}

function html_input_button($p){
	$p['type']='button';
	return html_input($p);
}

function html_input_enum($p){
	$p['tagName']='select';
	$p['innerHTML']='';
	if(!isset($p['value']))$p['value']=NULL;
	foreach($p['enum'] as $k=>$v){
		if(($p['value']==$k) || ($p['value']==$v))$selected='SELECTED';else $selected='';
		$p['innerHTML'].="<option value='$k' $selected>$v</option>";
	};
	return html_input($p);
}

function html_input_float($p){
	$p['type']='number';
	return html_input($p);
}

function html_input_varchar($p){
	if(isset($p['size']))$p['length']=$p['size'];
	return html_input($p);
}

function html_input_integer($p){
	$p['type']='number';
	return html_input($p);
}

function html_input_tinyint($p){
	$p['type']='number';
	return html_input($p);
}



function html_input_int($p){
	return html_input_integer($p);
}

function html_input_text($p){
	if(isset($p['type']))unset($p['type']);
	$p['tagName']='textarea';
	$value='';
	if(isset($p['value'])){
		$value=$p['value'];
		unset($p['value']);
	};
	
	if(!isset($p['class']))$p['class']='dbinput';
	return html_tag($p).$value.html_tag(Array('tagName'=>'textarea','closing'=>1));
}

function html_input_longtext($p){
	return html_input_text($p);
}

function html_input_hidden($p){
	$p['type']='hidden';
	return html_input($p);
}

function html_input_timestamp($p){
	if(!isset($p['value']) && isset($p['default']))$p['value']=$p['default'];
	if(isset($p['value']) && is_number($p['value']))$p['value']=date('Y.m.d.H:i:s',(int)$p['value']);
	return html_input_varchar($p);
}

function html_input_date($p){
	if(isset($p['value']) && $p['value'])$p['value']=str_replace('-','.',$p['value']).'.';
	$p['class']='dbinput datepicker';	
	return html_input_varchar($p);	
}

function html_input_boolean($p){
	$p['type']='checkbox';
	if(!isset($p['value'])){
		if(isset($p['default']) && $p['default'])$p['value']=1;else $p['value']=0; 
	}
	if($p['value'])$p['checked']=1;else $p['checked']=0;
	return html_input($p);
}

function code_tag($s){
	return '<code>'.$s.'</code>';
}

function a_href($p){
	$p['tagName']='a';
	if(isset($p['value'])){
		$p['innerHTML']=$p['value'];
		unset($p['value']);
	}
	return html_tag($p);
}

function html_button($p){
	$p['tagName']='input';
	$p['type']='button';
	if(isset($p['href'])){
		$p['onclick']=js_redirect($p['href'],1);
		unset($p['href']);
	}
	if(isset($p['innerHTML'])){
		$p['value']=$p['innerHTML'];
		unset($p['innerHTML']);
	};
	return html_input($p);
}

function html_input_time($p){
	return html_input_varchar($p);
}

function html_input_submit($p=Array()){
	if(!isset($p['value']))$p['value']='Save';
	$p['tagName']='input';
	$p['type']='submit';
	if(!isset($p['class']))$p['class']='dbinput';
	return html_input($p);
	
}

function error_span($err){
	echo '<span class="error_span">'.$err.'</span>';
}

function html_input_captcha($p=Array()){
	$o='<img src="/captcha.jpg" class="captcha" alt="captcha" />';
	$p['type']='varchar';
	$p['class']='dbinput captchainput';
	return html_input_varchar($p).$o;
}
